package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsLibrarypositiongroup;
import com.jy.wms.service.WmsLibrarypositiongroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: WmsLibrarypositiongroupController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author syh
 * @Date Thu Sep 28 10:59:21 CST 2017
 */
@Controller
@RequestMapping("/wmslibrarypositiongroup")
@Api(value = "/wmslibrarypositiongroup", description = "关于库位组管理操作")
public class WmsLibrarypositiongroupController extends BaseController<WmsLibrarypositiongroupService,WmsLibrarypositiongroup> {
    /**
     * 查询库位组信息列表
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value="/list")
    @ApiOperation(value = "库位组管理 POST请求查询库位组信息",response = JSONObject.class, notes = "POST请求查询库位组信息")
    public @ResponseBody JSONObject getWmsLibrarypositiongroup(@RequestBody WmsLibrarypositiongroup entity,HttpSession session) {
        Page<WmsLibrarypositiongroup> list = new Page<WmsLibrarypositiongroup>();
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            entity.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
                list=baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查货主名称
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value="/listhzmc")
    @ApiOperation(value = "库位组管理 新增库位组信息",response = JSONObject.class, notes = "查询输入{\"mc\":\"货主名称模糊查询\"}")
    public  @ResponseBody JSONObject addSearch(@RequestBody Map<String,Object> map,HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            Integer h=hz.size();
                if(h>0){
                    json.put("show_sshzid",hz.get(0).getShow_hzid());//货主名称
                    json.put("sshzid",hz.get(0).getHzid());//货主编号
                }else {
                    json.put("show_sshzid","");//货主名称
                    json.put("sshzid","");//货主编号
                }
            return json;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * (添加专用库位方法)查询库位接口,查询未在库位组下的库位集合
     * @param wmsLibraryPosition
     * @return
     */
    @PostMapping(value="/listkw")
    @ApiOperation(value = "库位组管理 POST请求查询库位信息",response = JSONObject.class, notes = "POST请求查询库位信息")
    public  @ResponseBody JSONObject getWmsLibraryposition(@RequestBody WmsLibraryPosition wmsLibraryPosition,HttpSession session) {
        Page<WmsLibraryPosition> list = new Page<WmsLibraryPosition>();
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            wmsLibraryPosition.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                wmsLibraryPosition.setSshzid(hz.get(0).getHzid());
                list= baseService.seachKw(wmsLibraryPosition);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 新增库位组
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "库位组信息 新增库位组信息",response = JSONObject.class, notes = "POST新增库位组信息")
    public @ResponseBody JSONObject  addWmsLibrarypositiongroup(@RequestBody WmsLibrarypositiongroup entity,HttpSession session) {
        JSONObject json=new JSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
            WmsLibrarypositiongroup entitys=baseService.searchAdd(entity.getMc(),ckid,entity.getSshzid());
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"新增库位组名称重复","新增库位组名称重复","");
            }else{
                entity.setId(UUIDUtils.uuid());
                entity.setCkid(ckid);
                baseService.baseInsert(entity);
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增库位组成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增库位组失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增库位组失败","新增库位组失败","");
        }
        return json;
    }
    /**
     * 编辑查询
     * @param id
     * @param session
     * @return
     */
    @GetMapping (value = "/getWmsLibrarypositiongroup/{id}")
    @ApiOperation(value = "库位组信息 查看库位组信息",response = JSONObject.class, notes = "GET查看库位组信息")
    public @ResponseBody JSONObject getWmsLibrarypositiongroupById(@PathVariable("id") String id, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            //查询是否激活
            baseService.selectBySfyx(id);
            WmsLibrarypositiongroup entity = baseService.baseSelectByPrimaryKey(id);
            json= JSONObject.fromObject(entity);//传对象
        }catch (ServiceException e) {
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个库位组信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个库位组信息失败","查看单个库位组信息失败","");
        }
        return json;
    }
    /**
     * 查看编辑已有库位信息 查库位组已有的库位集合ids
     * @param session
     * @return
     */
    @PostMapping (value = "/getWmsLibrarypositionIds")
    @ApiOperation(value = "库位信息 查看编辑已有库位信息",response = JSONObject.class, notes = "POST查看编辑已有库位信息")
    public @ResponseBody JSONObject getWmsLibrarypositionIds(@RequestBody WmsLibraryPosition wmsLibraryPosition, HttpSession session) {
        JSONObject json = new JSONObject();
        try {

            Page<WmsLibraryPosition> stringList = baseService.getWmsLibrarypositionIds(wmsLibraryPosition);
            json = SetStatus.setSearchType(JSONArray.fromObject(stringList).toString(),(int) stringList.getTotal());

        }catch (ServiceException e) {
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看编辑已有库位信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看编辑已有库位信息失败","查看编辑已有库位信息失败","");
        }
        return json;
    }

    /**
     * 查看库位按钮
     * @param wmsLibraryPosition
     * @param session
     * @return
     */
    @PostMapping(value = "/getPosition")
    @ApiOperation(value = "库位信息 查看编辑已有库位信息按钮",response = JSONObject.class, notes = "POST查看编辑已有库位信息按钮")
    public @ResponseBody JSONObject getPosition(@RequestBody WmsLibraryPosition wmsLibraryPosition, HttpSession session) {
        Page<WmsLibraryPosition> list = new Page<WmsLibraryPosition>();
        JSONObject json = new JSONObject();
        try {
            list=baseService.getPosition(wmsLibraryPosition);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除已有库位
     * @param wmsLibrarypositiongroup
     * @param session
     * @return
     */
    @PostMapping(value = "/deletePosition")
    @ApiOperation(value = "库位信息 删除已有库位信息",response = JSONObject.class, notes = "POST删除已有库位信息")
    public @ResponseBody JSONObject deletePosition(@RequestBody WmsLibrarypositiongroup wmsLibrarypositiongroup, HttpSession session){
        JSONObject json = new JSONObject();
        try{
           //删除该库位组下已有的库位
            baseService.deletePosition(wmsLibrarypositiongroup);
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "删除已有库位信息成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"删除已有库位信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除已有库位信息失败","删除已有库位信息失败","");
        }
        return json;
    }
    /**
     *
     *  修改查询库位专用(未在所有库位组的库位)
     * @param wmsLibraryPosition
     * @param session
     * @return
     */
    @PostMapping (value = "/getWmsLibrarypositionXg")
    @ApiOperation(value = "库位信息 查看库位信息",response = JSONObject.class, notes = "POST查看库位信息")
    public @ResponseBody JSONObject getWmsLibrarypositionXg(@RequestBody WmsLibraryPosition wmsLibraryPosition, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            wmsLibraryPosition.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                wmsLibraryPosition.setSshzid(hz.get(0).getHzid());
            }
            //全部库位 包含修改已有的库位和未在所有库位组的库位
            json =baseService.seachKw2(wmsLibraryPosition);
        }catch (ServiceException e) {
            e.printStackTrace();
            //return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个库位信息失败",e.getMessage(),"");
        }catch (Exception  e) {
            e.printStackTrace();
            //return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个库位信息失败","查看单个库位信息失败","");
        }
        return json;
    }

    /**
     * 编辑库位组信息
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "库位组信息 编辑保存库位组信息",response = JSONObject.class, notes = "POST编辑保存库位组信息")
    public @ResponseBody JSONObject editWmsLibrarypositiongroup(@RequestBody WmsLibrarypositiongroup entity, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            WmsLibrarypositiongroup entitys=baseService.searchAdds(entity.getMc(),entity.getId(),ckid,entity.getSshzid());
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"编辑库位组名称重复","编辑库位组名称重复","");
            }else {
                entity.setCkid(ckid);
                baseService.baseUpdateByPrimaryKeySelective(entity);
                   }
         }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑库位组成功","", JSONObject.fromObject(entity).toString());

        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑库位组失败",e.getMessage(),"");

        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑库位组失败","编辑库位组失败","");
        }
        return json;
    }

    /**
     * 编辑保存库位信息
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "/editPosition")
    @ApiOperation(value = "库位组信息 编辑库位信息",response = JSONObject.class, notes = "POST编辑库位信息")
    public @ResponseBody JSONObject editPosition(@RequestBody WmsLibrarypositiongroup entity, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
                entity.setCkid(ckid);
                baseService.editPosition(entity);

        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑库位成功","", JSONObject.fromObject(entity).toString());

        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑库位失败",e.getMessage(),"");

        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑库位失败","编辑库位失败","");
        }
        return json;
    }

    /**
     * 已激活接口
     * @param setModel
     * @return
     */
    @PostMapping(value = "yjh")
    @ApiOperation(value = "激活操作",response = JSONObject.class, notes = "数据激活操作")
    public @ResponseBody  JSONObject yjh(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = new JSONObject();
        try{
            //1:激活
            baseService.sfyx(setModel.getIds(),"1");
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "激活操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"激活操作失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"激活操作失败","操作失败","");
        }
        return json;
    }

    /**
     * 未激活接口
     * @param setModel
     * @return
     */
    @PostMapping(value = "bjh")
    @ApiOperation(value = "取消激活操作",response = JSONObject.class, notes = "数据取消激活操作")
    public @ResponseBody  JSONObject bjh(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = new JSONObject();
        try{
            //0:未激活状态
            baseService.sfyx(setModel.getIds(),"0");
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "取消激活操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"取消激活操作失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"取消激活操作失败","取消激活操作失败","");
        }
        return json;
    }

}
